package com.lfs.admin.service.impl;

import com.lfs.admin.dao.ChannelGroupDao;
import com.lfs.admin.model.entity.channel.ChannelGroupEntity;
import com.lfs.admin.model.entity.channel.ChannelToGroupEntity;
import com.lfs.admin.model.vo.channel.ChannelGroupVO;
import com.lfs.admin.model.vo.channel.ChannelToGroupVO;
import com.lfs.admin.service.ChannelGroupService;
import com.lfs.admin.service.ChannelGroupService;
import com.lfs.base.exception.BusinessException;
import com.lfs.common.constant.CommonConstants;
import com.lfs.dao.service.SystemService;
import com.lfs.interfaces.log.service.LogFileService;
import com.lfs.interfaces.model.vo.LogFileVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChannelGroupServiceImpl implements ChannelGroupService {

    private Logger logger = LoggerFactory.getLogger(ChannelGroupServiceImpl.class);

    @Autowired
    private ChannelGroupDao channelGroupDao;

    @Autowired
    private LogFileService logFileService;

    @Override
    public ChannelGroupEntity getChannelGroupById(Integer id) {
        return channelGroupDao.getChannelGroupById(id);
    }

    @Override
    public List<ChannelGroupEntity> queryChannelGroupList(ChannelGroupVO channelGroupVO) {
        return channelGroupDao.queryChannelGroupList(channelGroupVO);
    }

    @Override
    public List<ChannelToGroupEntity> getChannelToGroup(ChannelToGroupVO productToGroupVo) {
        return channelGroupDao.getChannelToGroup(productToGroupVo);
    }


    @Override
    public int updateChannelGroup(ChannelGroupVO channelGroupVO) {
        if(null != channelGroupVO.getIsDefault() && channelGroupVO.getIsDefault() == 1){
            if(queryChannelGroupList(new ChannelGroupVO()).stream().filter(channel -> channel.getIsDefault() == channelGroupVO.getIsDefault()).findAny().isPresent()){
                throw new BusinessException("已存在默认通道,无法修改多个!");
            }
        }
        return channelGroupDao.updateChannelGroup(channelGroupVO);
    }

    @Override
    public int updateWeight(ChannelToGroupVO channelToGroupVO) {
        return channelGroupDao.updateWeight(channelToGroupVO);
    }


    @Override
    public int addChannelGroup(ChannelGroupVO channelGroupVO) {
        int result = 0;
        try {
            ChannelGroupEntity channelGroupEntity = channelGroupDao.getChannelGroupById(channelGroupVO.getId());
            if (null != channelGroupEntity) {
                throw new BusinessException("该通道组编码已被存在!");
            } else {
                result = channelGroupDao.addChannelGroup(channelGroupVO);
                if (result > 0) {

                    String logRemark = "新增通道组编码为"+channelGroupVO.getId();
                    String loginName = SystemService.getCurrentUser().getLoginName() == null ? "error" :SystemService.getCurrentUser().getLoginName();
                    LogFileVO logFile = new LogFileVO();
                    logFile.setLogType(CommonConstants.LOG_ADD_TYPE);
                    logFile.setModifyType(CommonConstants.LOG_CHANNEL_GROUP_TYPE);
                    logFile.setIpAddr(this.getClass().getName());
                    logFile.setLogDetail(logRemark);
                    logFile.setLogRemark(logRemark);
                    logFile.setLoginName(loginName);
                    logFile.setAfterUpdate("");
                    logFile.setBeforeUpdate("");
                    logFileService.insertLogFile(logFile);
                    return result;
                } else {
                    throw new BusinessException("添加通道信息失败");
                }
            }
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public int addChannelToGroup(ChannelGroupVO channelGroupVO) {
        int result = 0;
        List<ChannelToGroupVO> channelToGroupList = new ArrayList<>();
        try {
            channelGroupVO.getChannelNums().forEach(channelNum->{
                ChannelToGroupEntity productToGroup = channelGroupDao.getChannelToGroupByCode(channelGroupVO.getId(),Long.valueOf(channelNum));
                if(null != productToGroup){
                    logger.warn("该产品编码={}已存在于产品组中,请勿重复添加!",channelNum);
                }else{
                    ChannelToGroupVO addVo = new ChannelToGroupVO();
                    addVo.setChannelGroupId(channelGroupVO.getId());
                    addVo.setChannelNum(channelNum);
                    channelToGroupList.add(addVo);
                }
            });
            result = channelGroupDao.addChannelToGroup(channelToGroupList);
            if (result > 0) {
                String logRemark = "产品组关联产品编码为"+channelGroupVO.getId()+"-"+channelGroupVO.getChannelNums();
                String loginName = SystemService.getCurrentUser().getLoginName() == null ? "error" :SystemService.getCurrentUser().getLoginName();
                LogFileVO logFile = new LogFileVO();
                logFile.setLogType(CommonConstants.LOG_ADD_TYPE);
                logFile.setModifyType(CommonConstants.LOG_CHANNEL_GROUP_TYPE);
                logFile.setIpAddr(this.getClass().getName());
                logFile.setLogDetail(logRemark);
                logFile.setLogRemark(logRemark);
                logFile.setLoginName(loginName);
                logFile.setAfterUpdate("");
                logFile.setBeforeUpdate("");
                logFileService.insertLogFile(logFile);
                return result;
            } else {
                throw new BusinessException("添加产品信息失败");
            }
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public int deleteChannelGroup(Integer[] ids){
        return channelGroupDao.deleteChannelGroup(ids);
    }

    @Override
    public int deleteChannelToGroup(Integer[] ids) {
        return channelGroupDao.deleteChannelToGroup(ids);
    }
}
